package com.example.note.cardlistview;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.androidkun.PullToRefreshRecyclerView;
import com.androidkun.callback.PullToRefreshListener;
import com.axdac.mixbind.TimeLine;
import com.example.note.R;

import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/28 0028.
 */

public class RCommAdapterListActivity extends AppCompatActivity {
    private PullToRefreshRecyclerView pullToRefreshRecyclerView;
    private CommonAdapter<JSONObject> commonAdapter ;
    private List<CommonAdapter.DataHandler<JSONObject>> handlers;
    private List<JSONObject> items;
    private Intent intent;
    private RCActivityPara para;
    public static final String paraName= "RCommAdapterListActivity.paraName";
    private String tag = "RCommAdapter";

    @Override
    protected void onResume() {
        super.onResume();
        if(para!=null&&para.onResume!=null){
            timeLineHandler(para.onResume);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(para!=null&&para.onPause!=null){
            timeLineHandler(para.onPause);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)  {
        super.onCreate(savedInstanceState);
        intent = getIntent();
        para= (RCActivityPara) intent.getSerializableExtra(paraName);

        setContentView(R.layout.activity_rcommadapterlist);
        pullToRefreshRecyclerView = (PullToRefreshRecyclerView) findViewById(R.id.main_view);
        handlers = new ArrayList<>();
        items = new ArrayList<>();
        commonAdapter = new CommonAdapter(items,handlers);

        if(para.builder==null) {
            pullToRefreshRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        }else{
            try {
                Class c1 = Class.forName(para.builder);
                Object object = c1.newInstance();
                LayoutManagerBuilder builder = (LayoutManagerBuilder) object;
                pullToRefreshRecyclerView.setLayoutManager(builder.getLayoutManager(this));
            } catch (Exception e) {
                e.printStackTrace();
                pullToRefreshRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            }
        }
        //设置adapter
        pullToRefreshRecyclerView.setAdapter(commonAdapter);
        //设置Item增加、移除动画
        pullToRefreshRecyclerView.setItemAnimator(new DefaultItemAnimator());


        Log.d(tag,"get para :"+para);
        pullToRefreshRecyclerView.setLoadingMoreEnabled(para.LoadingMoreEnabled);
        pullToRefreshRecyclerView.setPullRefreshEnabled(para.isPullReflesh);
        pullToRefreshRecyclerView.displayLastRefreshTime(para.displayLastRefreshTime);

        //设置 数据Handler
        if(para!=null&&para.DataHandlers!=null){
            List<CommonAdapter.DataHandler<JSONObject>> handlers = new ArrayList<>();
            for(String classStr : para.DataHandlers){
                try {
                    Class c1 = Class.forName(classStr);
                    Object handler = c1.newInstance();
                    CommonAdapter.DataHandler handler1 = (CommonAdapter.DataHandler) handler;
                    handlers.add(handler1);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            commonAdapter.addHandlers(handlers);
        }

        if(para.onCreate!=null){
            timeLineHandler(para.onCreate);
        }

        pullToRefreshRecyclerView.setPullToRefreshListener(new PullToRefreshListener() {
            @Override
            public void onRefresh() {
                if(para!=null&&para.onPullReflesh!=null) {
                    timeLineHandler2(para.onPullReflesh);
                }
            }

            @Override
            public void onLoadMore() {
                if(para!=null&&para.onLoaddingMore!=null) {
                    timeLineHandler2(para.onLoaddingMore);
                }
            }
        });

        if(para!=null){
            try {
                ActionBar actionBar = getSupportActionBar();
                actionBar.setTitle(para.actionBarTitle);
                actionBar.setHomeButtonEnabled(true);
                actionBar.setLogo(para.actionBarlogo);
                actionBar.setDisplayUseLogoEnabled(true);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }

    public void timeLineHandler2(Object dataMaker){
        new TimeLine.Builder()
                .addlib(dataMaker)
                .addlib(new TimeLine.timeLinePackage() {
                    @TimeLine.TimeLineTick(level = Integer.MAX_VALUE, thread = TimeLine.TimeLineTick.FORGROUND)
                    public void reflesh(TimeLine.RunningMsg msg) {
                        if(msg.map.get("clear")!=null&&msg.map.get("clear").equals("true")){
                            msg.map.remove("clear");
                            commonAdapter.clear();
                        }
                        List<JSONObject> objects = (List<JSONObject>) msg.map.get("data");
                        pullToRefreshRecyclerView.setLoadMoreComplete();
                        commonAdapter.reflesh(objects);
                    }
                }).run();
    }

    public void timeLineHandler(Object dataMaker){
        new TimeLine.Builder()
                .addlib(dataMaker)
                .addlib(new TimeLine.timeLinePackage(){
                    @TimeLine.TimeLineTick(level = Integer.MAX_VALUE,thread = TimeLine.TimeLineTick.FORGROUND)
                    public void reflesh(TimeLine.RunningMsg msg){
                        if(msg.map.get("clear")!=null&&msg.map.get("clear").equals("true")){
                            msg.map.remove("clear");
                            commonAdapter.clear();
                        }
                        List<JSONObject> objects = (List<JSONObject>) msg.map.get("data");
                        commonAdapter.reflesh(objects);
                    }
                }).run();
    }

    public static class RCActivityPara implements Serializable {
        public boolean isPullReflesh = false;
        public boolean LoadingMoreEnabled = false;
        public boolean displayLastRefreshTime =false;
        public String[] DataHandlers;

        public Object onCreate;
        public Object onPause;
        public Object onResume;
        public Object onPullReflesh;
        public Object onLoaddingMore;
        public String builder;


        public String actionBarTitle="设置";
        public int actionBarlogo=0;
    }

    public interface LayoutManagerBuilder extends Serializable {
        RecyclerView.LayoutManager getLayoutManager(Context context);
    }

}
